package com.yun.blog.controller;

import com.yun.blog.bean.Result;
import com.yun.blog.utils.FileUtils;
import com.yun.blog.utils.QianiuUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 测试接口
 * title        : TestController
 * projectName  : dantouBlog-back
 * description  : 测试控制类
 * author       : simple
 * date         : 2022/11/24下午2:07
 */

@Slf4j
@RestController
@RequestMapping("/test")
@Api(tags = "测试接口")
public class TestController {

    @RequestMapping(value = "/test", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation("测试连接是否正常")
    public Result test() {
        return Result.success("连接成功！");
    }

    @GetMapping("/getKaptchaCode")
    @ApiOperation("获取验证码")
    public String getKaptchaCode(HttpSession session) {
        Object kaptchaSessionKey = session.getAttribute("KAPTCHA_SESSION_KEY");
        // log.info("KaptchaCode: {}", kaptchaSessionKey);
        return (String) kaptchaSessionKey;
    }

    @PostMapping("/testQianNiuUpload")
    @ApiOperation(value = "测试七牛云上传文件", hidden = true)
    public Result testQianNiuUpload(MultipartFile file) {
        Result fail = Result.fail();
        if (file == null || file.isEmpty()) {
            fail.setMsg("图片不能为空！");
            return fail;
        }
        String filename = file.getOriginalFilename();
        boolean allowPic = FileUtils.isAllowPic(filename);
        if (!allowPic) {
            fail.setMsg("图片类型不正确！");
            return fail;
        }

        try {
            QianiuUploadUtils.upload(file, "test/upload");
            return Result.success("success");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/getSession")
    @ApiOperation("获取sessionId")
    public String getSession(HttpSession session) {
        // log.info("KaptchaCode: {}", kaptchaSessionKey);
        log.info("sessionId : {}",session.getId());
        return "success get session";
    }
}
